% Open txt. file to store results
fileID = fopen('household_calibration_results.txt','wt')

% Define global variables to feed parameter values into LMRT-18 simulation
% code. 
global delta_TS beta_TS rho_TS

% Specify the grid of parameter values to iterate over.
minimum_loss = 1440.03;

beta_min = 0.5022;
beta_max = 0.5038;
beta_grid = 9;

delta_min = 0.9872;
delta_max = 0.9888;
delta_grid = 9;

rho_min = 1.1041;
rho_max = 1.1061;
rho_grid = 11;


fprintf(fileID, ['CALIBRATION\r\n']);
fprintf(fileID, ['beta between %f and %f (%d values) \r\n'],[beta_min, beta_max, beta_grid]);
fprintf(fileID, ['delta between %f and %f (%d values) \r\n'],[delta_min, delta_max, delta_grid]);
fprintf(fileID, ['rho between %f and %f (%d values) \r\n'],[rho_min, rho_max, rho_grid]);
fprintf(fileID, ['********************\r\n']);


for beta = linspace(beta_min,beta_max,beta_grid)
    for delta = linspace(delta_min,delta_max,delta_grid)
        for rho = linspace(rho_min,rho_max,rho_grid)
            beta_TS = beta;
            delta_TS = delta;
            rho_TS = rho;
            batchmsm20160515dbr_naif1_singleRun;
            current_loss = ans;
            fprintf(fileID, ['********************\r\n']);
            fprintf(fileID, ['Loss function = %f \r\n'],[current_loss]);
            fprintf(fileID, ['beta = %f, delta = %f, rho = %f \r\n'],[beta, delta, rho]);
            if current_loss < minimum_loss
                minimum_loss = current_loss;
                beta_best = beta;
                delta_best = delta;
                rho_best = rho;
            end
        end
    end
end
fprintf(fileID, ['********************\r\n']);
fprintf(fileID, ['MINIMUM LOSS = %f \r\n'],[minimum_loss]);
fprintf(fileID, ['Achieved for beta = %f, delta = %f, rho = %f \r\n'],[beta_best, delta_best, rho_best]);
fprintf(fileID, ['********************\r\n']);
